package com.bluezone.dec.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bluezone.dec.common.CacheConstant;
import com.bluezone.dec.common.SystemConstant;
import com.bluezone.dec.configuration.AppConfig;
import com.bluezone.dec.system.service.HiddenTroubleInvestigationStatisticsService;
import com.bluezone.dec.system.vo.EnterpriseVo;
import com.bluezone.dec.utils.RestUtils;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author HHL
 * @description 隐患排查统计接口实现类
 * @date 2018/12/6 - 10:43
 */
@Service
public class HiddenTroubleInvestigationStatisticsServiceImpl implements HiddenTroubleInvestigationStatisticsService {

    @Autowired
    private AppConfig appConfig;

    @Autowired
    private RestUtils restUtils;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 获取隐患排查类别现场管理
     *
     * @return
     */
    @Override
    public List getSiteManagement() {

        if (Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.HIDDEN_DANGER_INSPECTION_SITE_MANAGEMENT))) {

            int i;
            String apiUrl = restUtils.getApiUrl("getSiteManagement");
            JSONArray siteManagement = restUtils.doGet(apiUrl);

            /* 隐患排查现场管理类别统计数据 */
            List<Map<String, Object>> siteManagementList = new ArrayList<>();

            for (i = 0; i < siteManagement.size(); i++) {

                JSONObject siteManagementMapObj = siteManagement.getJSONObject(i);
                Map<String, Object> siteManagementMap = new LinkedHashMap<>();

                siteManagementMap.put(SystemConstant.NAME, siteManagementMapObj.getString(SystemConstant.CHECK_NAME));
                siteManagementMap.put(SystemConstant.VALUE, siteManagementMapObj.getString(SystemConstant.HIDDEN_TROUBLE_INSPECTION_RECORD_COUNTS));

                siteManagementList.add(siteManagementMap);
            }

            /* 获取隐患排查类别现场管理redis缓存并设置缓存过期时间 */
            redisTemplate.opsForValue().set(CacheConstant.HIDDEN_DANGER_INSPECTION_SITE_MANAGEMENT,
                    JSON.toJSONString(siteManagementList),
                    appConfig.getChartDataCacheTime(),
                    TimeUnit.SECONDS);

            return siteManagementList;
        } else {
            String siteManagementList = redisTemplate.opsForValue().get(CacheConstant.HIDDEN_DANGER_INSPECTION_SITE_MANAGEMENT);
            return JSONArray.parseArray(siteManagementList, Map.class);
        }
    }

    /**
     * 获取隐患排查基础类别统计数据
     *
     * @return
     */
    @Override
    public List getBasicManagement() {

        if (Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.HIDDEN_DANGER_INSPECTION_BASIC_CATEGORY_STATISTIC))) {

            int i;
            String apiUrl = restUtils.getApiUrl("getBasicManagement");
            JSONArray basicManagement = restUtils.doGet(apiUrl);

            /* 隐患排查基础类别统计数据 */
            List<Map<String, Object>> basicManagementtList = new ArrayList<>();

            for (i = 0; i < basicManagement.size(); i++) {

                JSONObject basicManagementObj = basicManagement.getJSONObject(i);
                Map<String, Object> basicManagementMap = new LinkedHashMap<>();

                basicManagementMap.put(SystemConstant.NAME, basicManagementObj.getString(SystemConstant.CHECK_NAME));
                basicManagementMap.put(SystemConstant.VALUE, basicManagementObj.getString(SystemConstant.HIDDEN_TROUBLE_INSPECTION_RECORD_COUNTS));

                basicManagementtList.add(basicManagementMap);
            }

            /* 获取隐患排查基础类别统计数据redis缓存并设置缓存过期时间 */
            redisTemplate.opsForValue().set(CacheConstant.HIDDEN_DANGER_INSPECTION_BASIC_CATEGORY_STATISTIC,
                    JSON.toJSONString(basicManagementtList),
                    appConfig.getChartDataCacheTime(),
                    TimeUnit.SECONDS);

            return basicManagementtList;
        } else {
            String basicManagementtList = redisTemplate.opsForValue().get(CacheConstant.HIDDEN_DANGER_INSPECTION_BASIC_CATEGORY_STATISTIC);
            return JSONArray.parseArray(basicManagementtList, Map.class);
        }
    }

    /**
     * 获取隐患排查所有类别统计数据
     *
     * @return
     */
    @Override
    public Map<String, Object> getHiddenCategoryAllData() {

        /* 隐患排查类别[外圈] */
        List outList = this.getOutHiddenRiskInvestCategory();
        /* 隐患排查类别[内圈] */
        List innerList = this.getInnerHiddenRiskInvestCategory();

        Map<String, Object> allCategotyData = new HashMap<>();

        allCategotyData.put("insideValue", innerList);
        allCategotyData.put("outsideValue", outList);

        return allCategotyData;
    }

    /**
     * 隐患排查类别[外圈]
     *
     * @return
     */
    @Override
    public List getOutHiddenRiskInvestCategory() {

        if (Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.HIDDEN_DANGER_INSPECTION_CATEGORY_OUTSIDE))) {

            int i;
            String apiUrl = restUtils.getApiUrl("getOutHiddenRiskInvestCategory");
            JSONArray outHiddenRiskInvestCategory = restUtils.doGet(apiUrl);

            /* 隐患排查类别[外圈] */
            List<Map<String, Object>> outList = new ArrayList<>();

            for (i = 0; i < outHiddenRiskInvestCategory.size(); i++) {

                JSONObject outObj = outHiddenRiskInvestCategory.getJSONObject(i);
                Map<String, Object> outMap = new LinkedHashMap<>();

                outMap.put(SystemConstant.NAME, outObj.getString(SystemConstant.CHECK_NAME));
                outMap.put(SystemConstant.VALUE, outObj.getString(SystemConstant.HIDDEN_TROUBLE_INSPECTION_RECORD_COUNTS));

                outList.add(outMap);
            }

            /* 隐患排查类别[外圈]redis缓存并设置缓存过期时间 */
            redisTemplate.opsForValue().set(CacheConstant.HIDDEN_DANGER_INSPECTION_CATEGORY_OUTSIDE,
                    JSON.toJSONString(outList),
                    appConfig.getChartDataCacheTime(),
                    TimeUnit.SECONDS);

            return outList;
        } else {
            String outList = redisTemplate.opsForValue().get(CacheConstant.HIDDEN_DANGER_INSPECTION_CATEGORY_OUTSIDE);
            return JSONArray.parseArray(outList, Map.class);
        }
    }

    /**
     * 隐患排查类别[内圈]
     *
     * @return
     */
    @Override
    public List getInnerHiddenRiskInvestCategory() {

        if (Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.HIDDEN_DANGER_INSPECTION_CATEGORY_INNER))) {

            int i;
            String apiUrl = restUtils.getApiUrl("getInnerHiddenRiskInvestCategory");
            JSONArray innerHiddenRiskInvestCategory = restUtils.doGet(apiUrl);

            /* 隐患排查类别[内圈] */
            List<Map<String, Object>> innerList = new ArrayList<>();

            for (i = 0; i < innerHiddenRiskInvestCategory.size(); i++) {

                JSONObject innerObj = innerHiddenRiskInvestCategory.getJSONObject(i);

                if ("1".equals(innerObj.getString("type"))) {

                    Map<String, Object> innerMap = new LinkedHashMap<>();

                    innerMap.put(SystemConstant.NAME, "安全管理");
                    innerMap.put(SystemConstant.VALUE, innerObj.getString(SystemConstant.HIDDEN_TROUBLE_INSPECTION_RECORD_COUNTS));

                    innerList.add(innerMap);
                } else {
                    Map<String, Object> tempMap = new HashMap<>();

                    tempMap.put(SystemConstant.NAME, "现场管理");
                    tempMap.put(SystemConstant.VALUE, innerObj.getString(SystemConstant.HIDDEN_TROUBLE_INSPECTION_RECORD_COUNTS));

                    innerList.add(tempMap);
                }
            }

            /* 隐患排查类别[内圈]redis缓存并设置缓存过期时间 */
            redisTemplate.opsForValue().set(CacheConstant.HIDDEN_DANGER_INSPECTION_CATEGORY_INNER,
                    JSON.toJSONString(innerList),
                    appConfig.getChartDataCacheTime(),
                    TimeUnit.SECONDS);

            return innerList;
        } else {
            String innerList = redisTemplate.opsForValue().get(CacheConstant.HIDDEN_DANGER_INSPECTION_CATEGORY_INNER);
            return JSONArray.parseArray(innerList, Map.class);
        }
    }

    /**
     * 隐患企业自查
     *
     * @return
     */
    @Override
    public List getEnterpriseSelfCheck() {

        if (Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.HIDDEN_DANGER_ENTERPRISE_SELF_CHECK))) {

            String apiUrl = restUtils.getApiUrl("getEnterpriseLawSelfCheck");
            List<JSONObject> inspectionArray = restUtils.doGet(apiUrl).toJavaList(JSONObject.class);
            List<EnterpriseVo> enterpriseList = this.getAllHiddenLawInspectEnterprises();

            /* 企业自查 */
            List<Map<String, Object>> entSelfCheckList = new ArrayList<>();

            if (!enterpriseList.isEmpty()) {

                /* 取出ENT_BUSINESSINFO表中存在的所有企业名字 */
                List<String> entSelfCheckNameList = enterpriseList.stream().map(EnterpriseVo::getEntName).collect(Collectors.toList());
                this.handleData(entSelfCheckNameList, inspectionArray, entSelfCheckList);
            }

            /* 隐患企业自查redis缓存并设置缓存过期时间 */
            redisTemplate.opsForValue().set(CacheConstant.HIDDEN_DANGER_ENTERPRISE_SELF_CHECK,
                    JSON.toJSONString(entSelfCheckList),
                    appConfig.getChartDataCacheTime(),
                    TimeUnit.SECONDS);

            return entSelfCheckList;
        } else {
            String entSelfCheckList = redisTemplate.opsForValue().get(CacheConstant.HIDDEN_DANGER_ENTERPRISE_SELF_CHECK);
            return JSONArray.parseArray(entSelfCheckList, Map.class);
        }
    }

    /**
     * 获取隐患执法检查
     *
     * @return
     */
    @Override
    public List getLawEnforcementInspection() {

        if (Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.HIDDEN_DANGER_LAW_ENFORCE_INSPECTION))) {

            String apiUrl = restUtils.getApiUrl("getLawEnforcementInspection");
            /* 获取政府巡查数据 */
            List<JSONObject> inspectionArray = restUtils.doGet(apiUrl).toJavaList(JSONObject.class);
            List<EnterpriseVo> enterpriseList = this.getAllHiddenLawInspectEnterprises();

            /* 企业自查 */
            List enterpriseSelfCheckList = this.getEnterpriseSelfCheck();
            Map<String, Object> entSelfCheckMap = new LinkedHashMap<>();

            entSelfCheckMap.put(SystemConstant.RECTIFICATION_NAME, "企业自查");
            entSelfCheckMap.put("ent", enterpriseSelfCheckList);

            /* 政府巡查 */
            List<Map<String, Object>> govCheckList = new ArrayList<>();
            Map<String, Object> govCheckMap = new LinkedHashMap<>();

            List<Map<String, Object>> finalResultList = new ArrayList<>();

            if (!enterpriseList.isEmpty()) {

                /* 取出ENT_BUSINESSINFO表中存在的所有企业名字 */
                List<String> entNameList = enterpriseList.stream().map(EnterpriseVo::getEntName).collect(Collectors.toList());
                this.handleData(entNameList, inspectionArray, govCheckList);

                govCheckMap.put(SystemConstant.RECTIFICATION_NAME, "政府巡查");
                govCheckMap.put("ent", govCheckList);
            }

            finalResultList.add(entSelfCheckMap);
            finalResultList.add(govCheckMap);

            /* 获取隐患执法检查redis缓存并设置缓存过期时间 */
            redisTemplate.opsForValue().set(CacheConstant.HIDDEN_DANGER_LAW_ENFORCE_INSPECTION,
                    JSON.toJSONString(finalResultList),
                    appConfig.getChartDataCacheTime(),
                    TimeUnit.SECONDS);

            return finalResultList;
        } else {
            String finalResultList = redisTemplate.opsForValue().get(CacheConstant.HIDDEN_DANGER_LAW_ENFORCE_INSPECTION);
            return JSONArray.parseArray(finalResultList, Map.class);
        }
    }

    /**
     * 获取隐患执法检查_获取所有隐患执法企业
     *
     * @return
     */
    @Override
    public List<EnterpriseVo> getAllHiddenLawInspectEnterprises() {

        if (Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.ALL_HIDDEN_DANGER_LAW_ENFORCE_ENTERPRISES))) {

            String apiUrl = restUtils.getApiUrl("getAllHiddenLawInspectEnterprises");
            JSONArray enterprisesArray = restUtils.doGet(apiUrl);
            List<EnterpriseVo> enterprieList = new ArrayList<>();

            if (!enterprisesArray.isEmpty()) {
                enterprieList = JSONArray.parseArray(enterprisesArray.toJSONString(), EnterpriseVo.class);
            }

            /* 获取隐患执法检查_获取所有隐患执法企业redis缓存并设置缓存过期时间 */
            redisTemplate.opsForValue().set(CacheConstant.ALL_HIDDEN_DANGER_LAW_ENFORCE_ENTERPRISES,
                    JSON.toJSONString(enterprieList),
                    appConfig.getChartDataCacheTime(),
                    TimeUnit.SECONDS);

            return enterprieList;
        } else {
            String enterprieList = redisTemplate.opsForValue().get(CacheConstant.ALL_HIDDEN_DANGER_LAW_ENFORCE_ENTERPRISES);
            return JSONArray.parseArray(enterprieList, EnterpriseVo.class);
        }
    }

    /**
     * 获取隐患整改统计
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getHiddenDangerReformInfo() {

        List<Map<String, Object>> finalResulList = new ArrayList<>();

        Map<String, Object> unCheckRsultList = this.getUnCheckListInfo();
        Map<String, Object> checkRsultList = this.getCheckListInfo();
        Map<String, Object> totalResultMapList = this.getTotalHiddenTrouble();

        finalResulList.add(unCheckRsultList);
        finalResulList.add(checkRsultList);
        finalResulList.add(totalResultMapList);

        return finalResulList;
    }

    /**
     * 获取隐患整改统计---隐患总数
     *
     * @return
     */
    @Override
    public Map<String, Object> getTotalHiddenTrouble() {

        if (Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.TOTAL_NUMBER_OF_HIDDEN_DANGER))) {

            int i;
            String apiUrl = restUtils.getApiUrl("getTotalHiddenTrouble");
            JSONArray jsonArray = restUtils.doGet(apiUrl);

            List<Map<String, Object>> totalHiddenTroubleMapList = new ArrayList<>();
            Map<String, Object> totalHiddenTroubleMap = new HashMap<>();

            for (i = 0; i < jsonArray.size(); i++) {

                JSONObject reformJsonObject = jsonArray.getJSONObject(i);

                Map<String, Object> reformMap = new HashMap<>();
                reformMap.put(SystemConstant.NAME, reformJsonObject.getString(SystemConstant.ENTERPRISE_NAME));
                reformMap.put(SystemConstant.VALUE, reformJsonObject.getString(SystemConstant.FORM_NUMBER));
                totalHiddenTroubleMapList.add(reformMap);
            }

            totalHiddenTroubleMap.put(SystemConstant.RECTIFICATION_NAME, "隐患总数");
            totalHiddenTroubleMap.put("ent", totalHiddenTroubleMapList);

            /* 获取隐患整改统计---隐患总数redis缓存并设置缓存过期时间 */
            redisTemplate.opsForValue().set(CacheConstant.TOTAL_NUMBER_OF_HIDDEN_DANGER,
                    JSON.toJSONString(totalHiddenTroubleMap),
                    appConfig.getChartDataCacheTime(),
                    TimeUnit.SECONDS);

            return totalHiddenTroubleMap;
        } else {
            String totalHiddenTroubleMap = redisTemplate.opsForValue().get(CacheConstant.TOTAL_NUMBER_OF_HIDDEN_DANGER);
            return JSON.parseObject(totalHiddenTroubleMap);
        }
    }

    /**
     * 获取隐患整改统计---已复查列表
     *
     * @return
     */
    @Override
    public Map<String, Object> getCheckListInfo() {

        if (Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.HIDDEN_DANGER_REFORM_RECHECKED))) {

            int i;
            int j;
            String apiUrl = restUtils.getApiUrl("getCheckListInfo");
            JSONArray rowsArray = restUtils.doGet(apiUrl);

            List<EnterpriseVo> enterpriseVoList = this.getAllHiddenRectEnterprises();
            List<String> enterpriseNameList = enterpriseVoList.stream().map(EnterpriseVo::getEntName).collect(Collectors.toList());

            List<Object> checkCountMapList = new ArrayList<>();
            Map<String, Object> checkMap = new HashMap<>();

            for (j = 0; j < enterpriseNameList.size(); j++) {

                Map<String, Object> checkedMap = new HashMap<>();

                /* 设置初始已复查的企业记录数为 0 */
                checkedMap.put(SystemConstant.NAME, enterpriseNameList.get(j));
                checkedMap.put(SystemConstant.VALUE, 0);

                /* 遍历返回的包含ent_name、对应公司对应整改状态的数量统计List */
                for (i = 0; i < rowsArray.size(); i++) {

                    JSONObject reformJsonObject = rowsArray.getJSONObject(i);

                    String enterName = enterpriseNameList.get(j);
                    String enterpriseName = reformJsonObject.getString(SystemConstant.ENTERPRISE_NAME);

                    if (enterName.equals(enterpriseName)) {
                        checkedMap.put(SystemConstant.VALUE, reformJsonObject.getString(SystemConstant.FORM_NUMBER));
                    }
                }

                checkCountMapList.add(checkedMap);
            }

            checkMap.put(SystemConstant.RECTIFICATION_NAME, "已复查");
            checkMap.put("ent", checkCountMapList);

            /* 添加获取隐患整改统计---已复查列表redis缓存并设置缓存过期时间 */
            redisTemplate.opsForValue().set(CacheConstant.HIDDEN_DANGER_REFORM_RECHECKED,
                    JSON.toJSONString(checkMap),
                    appConfig.getChartDataCacheTime(),
                    TimeUnit.SECONDS);

            return checkMap;
        } else {
            String checkMap = redisTemplate.opsForValue().get(CacheConstant.HIDDEN_DANGER_REFORM_RECHECKED);
            return JSON.parseObject(checkMap);
        }
    }


    /**
     * 获取隐患整改统计---未复查列表
     *
     * @return
     */
    @Override
    public Map<String, Object> getUnCheckListInfo() {

        if (Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.HIDDEN_DANGER_REFORM_UNRECHECK))) {

            int i;
            int j;
            String apiUrl = restUtils.getApiUrl("getUnCheckListInfo");
            JSONArray rowsArray = restUtils.doGet(apiUrl);

            List<EnterpriseVo> enterpriseVoList = this.getAllHiddenRectEnterprises();
            List<String> enterpriseNameList = enterpriseVoList.stream().map(EnterpriseVo::getEntName).collect(Collectors.toList());

            List<Object> unCheckCountMapList = new ArrayList<>();
            Map<String, Object> unCheckMap = new HashMap<>();

            /* 遍历返回的包含ent_name、对应公司对应整改状态的数量统计List */
            for (j = 0; j < enterpriseNameList.size(); j++) {

                Map<String, Object> unCheckedMap = new HashMap<>();

                /* 设置初始未复查的企业记录数为 0 */
                unCheckedMap.put(SystemConstant.NAME, enterpriseNameList.get(j));
                unCheckedMap.put(SystemConstant.VALUE, 0);

                for (i = 0; i < rowsArray.size(); i++) {

                    JSONObject reformJsonObject = rowsArray.getJSONObject(i);

                    String enterName = enterpriseNameList.get(j);
                    String enterpriseName = reformJsonObject.getString(SystemConstant.ENTERPRISE_NAME);

                    /* 如果企业中存在未复查企业，重新设置该企业的未复查的记录数值 */
                    if (enterName.equals(enterpriseName)) {
                        unCheckedMap.put(SystemConstant.VALUE, reformJsonObject.getString(SystemConstant.FORM_NUMBER));
                    }
                }

                unCheckCountMapList.add(unCheckedMap);
            }

            unCheckMap.put(SystemConstant.RECTIFICATION_NAME, "未复查");
            unCheckMap.put("ent", unCheckCountMapList);

            /* 添加获取隐患整改统计---未复查列表redis缓存并设置缓存过期时间 */
            redisTemplate.opsForValue().set(CacheConstant.HIDDEN_DANGER_REFORM_UNRECHECK,
                    JSON.toJSONString(unCheckMap),
                    appConfig.getChartDataCacheTime(),
                    TimeUnit.SECONDS);

            return unCheckMap;
        } else {
            String unCheckMap = redisTemplate.opsForValue().get(CacheConstant.HIDDEN_DANGER_REFORM_UNRECHECK);
            return JSON.parseObject(unCheckMap);
        }
    }

    /**
     * 获取隐患整改统计_获取两行表中所有记录相关联的企业
     *
     * @return
     */
    @Override
    public List<EnterpriseVo> getAllHiddenRectEnterprises() {

        if (Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.HIDDEN_DANGER_REFORM_RELATE_ENTERPRISE))) {

            String apiUrl = restUtils.getApiUrl("getAllHiddenRectEnterprises");
            JSONArray enterpriseArray = restUtils.doGet(apiUrl);

            /* 添加获取隐患整改统计_获取两行表中所有记录相关联的企业redis缓存并设置缓存过期时间 */
            redisTemplate.opsForValue().set(CacheConstant.HIDDEN_DANGER_REFORM_RELATE_ENTERPRISE,
                    JSON.toJSONString(enterpriseArray),
                    appConfig.getChartDataCacheTime(),
                    TimeUnit.SECONDS);

            if (!enterpriseArray.isEmpty()) {
                return JSONArray.parseArray(enterpriseArray.toJSONString(), EnterpriseVo.class);
            }
            return new ArrayList<>();
        } else {
            String enterpriseVoList = redisTemplate.opsForValue().get(CacheConstant.HIDDEN_DANGER_REFORM_RELATE_ENTERPRISE);
            return JSONArray.parseArray(enterpriseVoList, EnterpriseVo.class);
        }

    }

    /**
     * 获取隐患排查标题3_隐患整改情况统计
     *
     * @return
     */
    @Override
    public String getHiddenDangerReformCount() {

        if (Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.HIDDEN_DANGER_REFORM_STATISTIC))) {

            String apiUrl = restUtils.getApiUrl("getHiddenDangerReformCount");
            JSONArray hiddenDangerReformCountArray = restUtils.doGet(apiUrl);
            JSONObject hiddenDangerReformCount = hiddenDangerReformCountArray.getJSONObject(0);
            String hidDanReformCount = hiddenDangerReformCount.getString("hidDanReformCount");

            /* 添加获取隐患排查标题3_隐患整改情况统计redis缓存并设置缓存过期时间 */
            redisTemplate.opsForValue().set(CacheConstant.HIDDEN_DANGER_REFORM_STATISTIC,
                    hidDanReformCount,
                    appConfig.getChartDataCacheTime(),
                    TimeUnit.SECONDS);

            return Strings.isNullOrEmpty(hidDanReformCount) ? "0" : hidDanReformCount;
        } else {
            String hidDanReformCount = redisTemplate.opsForValue().get(CacheConstant.HIDDEN_DANGER_REFORM_STATISTIC);
            return Strings.isNullOrEmpty(hidDanReformCount) ? "0" : hidDanReformCount;
        }
    }

    /**
     * 获取隐患排查标题2_企业执法情况统计
     *
     * @return
     */
    @Override
    public String getEnterpriseLawEnforcementCount() {

        if (Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.ENTERPRISE_LAW_ENFORCEMENT))) {

            String apiUrl = restUtils.getApiUrl("getEnterpriseLawEnforcementCount");
            JSONArray entLawEnforcementCountArray = restUtils.doGet(apiUrl);
            JSONObject entLawEnforcementCount = entLawEnforcementCountArray.getJSONObject(0);
            String entLawEnforceCount = entLawEnforcementCount.getString("entLawEnforceCount");

            /* 添加获取隐患排查标题2_企业执法情况统计redis缓存并设置缓存过期时间 */
            redisTemplate.opsForValue().set(CacheConstant.ENTERPRISE_LAW_ENFORCEMENT,
                    entLawEnforceCount,
                    appConfig.getChartDataCacheTime(),
                    TimeUnit.SECONDS);

            return Strings.isNullOrEmpty(entLawEnforceCount) ? "0" : entLawEnforceCount;
        } else {
            String entLawEnforceCount = redisTemplate.opsForValue().get(CacheConstant.ENTERPRISE_LAW_ENFORCEMENT);
            return Strings.isNullOrEmpty(entLawEnforceCount) ? "0" : entLawEnforceCount;
        }
    }

    /**
     * 获取隐患排查标题1_隐患排查统计
     *
     * @return
     */
    @Override
    public String getHiddenTroubleInvestStatistics() {

        if (Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.INSPECTION_STATISTIC))) {

            String apiUrl = restUtils.getApiUrl("getHiddenDangerInvestCount");
            JSONArray hiddenDangerInvestCountArray = restUtils.doGet(apiUrl);
            JSONObject hiddenDangerInvestCount = hiddenDangerInvestCountArray.getJSONObject(0);
            String hidDangerInvestCount = hiddenDangerInvestCount.getString("hidDangerInvestCount");

            /* 添加获取隐患排查标题1_隐患排查统计redis缓存并设置缓存过期时间 */
            redisTemplate.opsForValue().set(CacheConstant.INSPECTION_STATISTIC,
                    hidDangerInvestCount,
                    appConfig.getChartDataCacheTime(),
                    TimeUnit.SECONDS);

            return Strings.isNullOrEmpty(hidDangerInvestCount) ? "0" : hidDangerInvestCount;
        } else {
            String hidDangerInvestCount = redisTemplate.opsForValue().get(CacheConstant.INSPECTION_STATISTIC);
            return Strings.isNullOrEmpty(hidDangerInvestCount) ? "0" : hidDangerInvestCount;
        }
    }

    private void handleData(List<String> entSelfCheckNameList, List<JSONObject> inspectionArray, List<Map<String, Object>> entSelfCheckList) {
        for (int j = 0; j < entSelfCheckNameList.size(); j++) {

            /* 默认企业自查记录为 0 */
            Map<String, Object> selfCheckMap = new HashMap<>();
            selfCheckMap.put(SystemConstant.NAME, entSelfCheckNameList.get(j));
            selfCheckMap.put(SystemConstant.VALUE, 0);
            String enterName = entSelfCheckNameList.get(j);
            Map<String,Object> map =  inspectionArray.stream()
                    .filter(m->enterName.equals(m.getString(SystemConstant.ENTERPRISE_NAME)))
                    .findAny()
                    .map(m->{
                        String insCount = m.getString("insCount");
                        selfCheckMap.put(SystemConstant.NAME, enterName);
                        selfCheckMap.put(SystemConstant.VALUE, insCount);
                        return selfCheckMap;
                    }).orElse(selfCheckMap);
            entSelfCheckList.add(map);
        }
    }
}
